using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ext.Net;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Configuration;
using System.Web.Security;
using Sio.Mdm.Types.Settings;
using Sio.Mdm.Entities;

namespace Sio.Mdm.Web.Controls
{
    public class UserControl : System.Web.UI.UserControl
    {

        #region Properties

        #region Private
        #region _DefaultGridPageSize
        private static int _DefaultGridPageSize = 20;
        #endregion
        #endregion

        #region Protected

        #region UserSession
        /// <summary>
        /// Get current user session for authorization when calling BL functions
        /// </summary>
        protected Sio.Mdm.Types.Session UserSession
        {
            get
            {
                return Sio.Mdm.Types.Session.Current;
            }
        }
        #endregion

        #region CurrentTheme
        private Ext.Net.Theme? _theme;
        protected virtual Ext.Net.Theme CurrentTheme
        {
            get
            {
                if (_theme == null)
                {
                    string theme = ConfigurationManager.AppSettings["Theme"];

                    try
                    {
                        if (theme != null)
                        {
                            Ext.Net.Theme cooliteTheme =
                                (Ext.Net.Theme)Enum.Parse(typeof(Ext.Net.Theme), theme);
                            _theme = cooliteTheme;
                        }
                        else
                        {
                            _theme = Ext.Net.Theme.Gray;
                        }
                    }
                    catch (Exception)
                    {
                        _theme = Ext.Net.Theme.Gray;
                    }
                }

                return _theme.Value;
            }
            set
            {
                Session["_theme"] = value;
            }
        }
        #endregion

        #region ScriptManager
        private Ext.Net.ResourceManager _sc;
        internal protected Ext.Net.ResourceManager ScriptManager
        {
            get
            {
                return ExtNet.ResourceManager;

                //if (_sc == null)
                //{
                //    _sc = 
                //    if (_sc == null)
                //    {
                //        _sc = new Ext.Net.ResourceManager();
                //        _sc.ID = "ScriptManager" + Guid.NewGuid();

                //        HtmlForm form = Form == null ? FindForm() : Form;

                //        // In cas some lame programer has failed to comply with rules
                //        if (form == null)
                //        {
                //            Controls.AddAt(0, _sc);
                //        }
                //        else
                //        {
                //            form.Controls.AddAt(0, _sc);
                //        }
                //    }
                //}

                //return _sc;
            }
        }
        #endregion

        #region DefaultGridPageSize
        internal protected int DefaultGridPageSize
        {
            get
            {
                return _DefaultGridPageSize;
            }
        }
        #endregion

        #region UserSettings
        private UserSettings _userSettings;
        /// <summary>
        /// Get user settings
        /// </summary>
        protected UserSettings UserSettings
        {
            get
            {
                if (null == _userSettings)
                {
                    _userSettings = EntityFactory.CreateInstance<UserSettings>();
                }

                return _userSettings;
            }
        }
        #endregion

        #endregion

        #region Private

        #region CurrentAspTheme
        private string CurrentAspTheme
        {
            get
            {
                return CurrentTheme.ToString();
            }
        }
        #endregion

        #region LocaleScript
        private string LocaleScript
        {
            get
            {
                string template = "~/script/locale/ext-lang-{0}.js";
                return String.Format(template, "en");
            }
        }
        #endregion

        #endregion

        #endregion


    }
}
